package com.erps.config;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import com.alibaba.fastjson.JSON;
import com.erps.CommonUtils.ActionResult;
import com.erps.CommonUtils.AppConfig;
import com.erps.CommonUtils.Config;
import com.erps.CommonUtils.DateUtils;
import com.erps.CommonUtils.JActiveTable;
import com.erps.CommonUtils.RequestUtils;
import com.erps.CommonUtils.Sequence;
import com.erps.CommonUtils.StringEx;
import com.erps.CommonUtils.UploadInfo;
import com.erps.CommonUtils.UploadPostFiles;
import com.erps.CommonUtils.ValueType;
import com.erps.dao.ErspPlatArgsDao;
import com.erps.dao.ErspSysOrgDao;
import com.erps.dao.ErspUserFavDao;
import com.erps.db.DBManager;
import com.erps.model.ResRequestModel;
import com.erps.sys.rest.ErspCodeValueRest;
import com.erps.sys.rest.ErspResInfoRest;
import com.erps.sys.rest.ErspSysOrgRest;
import com.erps.sys.rest.ErspSysUserRest;
import com.erps.sys.rest.PageData;
import com.erps.sys.rest.PageInfo;
import com.hmkj.Utils.mail.MailSender;
import com.hmkj.Utils.mail.MailSenderInfo;
import com.hmkj.Utils.mail.MailUtils;
import com.hmkj.entity.ErspPlatArgs;
import com.hmkj.entity.ErspResHuman;
import com.hmkj.entity.ErspUserFav;
import com.hmkj.entity.vo.ErspCodeValueVO;
import com.hmkj.entity.vo.ersp_res_info_vo;
import com.jfinal.core.Controller;
import com.jfinal.plugin.activerecord.Page;

public class ServicesController extends Controller {
	public void index() {
		render("index.ftl");
	}

	public void sys_org() {
		PageInfo ActivePage = RequestUtils.getPage(getRequest());
		ErspSysOrgRest vrest = new ErspSysOrgRest();

		PageData vret = vrest.Query(ActivePage.getPageno(), ActivePage.getPagesize(), "");
		setAttr("dbrows", vret.getKeylist());
		ActivePage.setRowcount(vret.getRowcount());
		ActivePage.setPageno(vret.getPageno());
		this.setAttr("pageinfo", ActivePage);
		render("sys_org.ftl");
	}

	public void sys_org_save() {
		ErspSysOrgDao dao = new ErspSysOrgDao();
		dao = (ErspSysOrgDao) RequestUtils.getFromRequest(dao, this.getRequest());
		String cKeyID = StringEx.getStr(getPara("id"));
		int iCode = 0;
		if (StringEx.isEmpty(cKeyID)) {
			dao.setFieldValue("create_time", DateUtils.getDayTimeNum());
			iCode = dao.save();
		} else {
			dao.RemoveField("create_time");
			dao.set__rowkeys(cKeyID);
			iCode = dao.save();
		}

		ActionResult ret;
		if (iCode == ValueType.SUCCESS)
			ret = ActionResult.Valid(ValueType.SUCCESS);
		else
			ret = ActionResult.Valid(ValueType.FAILURE);
		renderJson(JSON.toJSONString(ret));
	}

	public void sys_org_delete() {
		ErspSysOrgDao dao = new ErspSysOrgDao();
		dao = (ErspSysOrgDao) RequestUtils.getFromRequest(dao, this.getRequest());
		String cKeyID = StringEx.getStr(getPara("id"));

		int iCode = 0;
		if (!StringEx.isEmpty(cKeyID)) {
			dao.set__rowkeys(cKeyID);
			iCode = dao.del();
		}
		ActionResult ret;
		if (iCode == ValueType.SUCCESS)
			ret = ActionResult.Valid(ValueType.SUCCESS);
		else
			ret = ActionResult.Valid(ValueType.FAILURE);
		renderJson(JSON.toJSONString(ret));
	}

	public void sys_user() {
		PageInfo ActivePage = RequestUtils.getPage(getRequest());
		ErspSysUserRest vrest = new ErspSysUserRest();
		PageData vret = vrest.Query(ActivePage.getPageno(), ActivePage.getPagesize(), " (user_checked=0)");
		setAttr("dbrows", vret.getKeylist());
		ActivePage.setRowcount(vret.getRowcount());
		ActivePage.setPageno(vret.getPageno());
		setAttr("pageinfo", ActivePage);
		render("sys_user.ftl");
	}

	public void sys_user_edit() {
		ErspPlatArgsDao dao = new ErspPlatArgsDao();
		String cKeyID = StringEx.getStr(getPara("id"));
		ErspPlatArgs obj = dao.get(cKeyID);
		setAttr("vo", obj);
		render("sys_user_edit.ftl");
	}

	public void sysconfig() {
		ErspPlatArgsDao dao = new ErspPlatArgsDao();
		List<Map<String, String>> dtRows = dao.Query();
		this.setAttr("dbrows", dtRows);
		render("sysconfig.ftl");
	}

	public void sysconfig_edit() {
		ErspPlatArgsDao dao = new ErspPlatArgsDao();
		String cKeyID = StringEx.getStr(getPara("id"));
		ErspPlatArgs obj = dao.get(cKeyID);
		setAttr("vo", obj);
		render("sysconfig_edit.ftl");
	}

	public void left_1() {
		render("left_1.ftl");
	}

	public void zhsz() {
		render("zhsz.ftl");
	}

	public void search() {
		render("search.ftl");
	}

	public void nodes() {
		String cTypeID = StringEx.getStr(getPara("typeid"));
		setAttr("typeid", cTypeID);
		render("nodes.ftl");
	}

	public void left() {
		String cTypeID = StringEx.getStr(getPara("typeid"));
		setAttr("typeid", cTypeID);
		render("left.ftl");
	}

	public void myspace() {
		allreq();
	}

	public void ready_approval() {
		PageInfo ActivePage = RequestUtils.getPage(getRequest());
		ErspResInfoRest vrest = new ErspResInfoRest();
		PageData vret = vrest.Query(ActivePage.getPageno(), ActivePage.getPagesize(), " (res_checked='0') ");
		setAttr("dbrows", vret.getKeylist());
		ActivePage.setRowcount(vret.getRowcount());
		ActivePage.setPageno(vret.getPageno());
		this.setAttr("pageinfo", ActivePage);
		render("ready_approval.ftl");
	}

	public void doapproval() {
		String cResID = StringEx.getStr(this.getPara("id"));
		int iTypeID = StringEx.getInt(this.getPara("typeid"));
		ActionResult ret = ActionResult.Valid(ValueType.FAILURE);
		if (!StringEx.isEmpty(cResID)) {
			ErspResInfoRest dao = new ErspResInfoRest();
			int iCode = dao.ChangeApproval(cResID, iTypeID, StringEx.getStr(getSessionAttr(AppConfig.SESSION_USERID)));
			if (iCode > 0)
				ret = ActionResult.Valid(ValueType.SUCCESS);
		}
		renderJson(JSON.toJSONString(ret));
	}

	public void dofav() {
		ErspUserFavDao dao = new ErspUserFavDao();
		ErspUserFav vo = new ErspUserFav();
		vo.setUser_id(StringEx.getStr(getSessionAttr(AppConfig.SESSION_USERGUID)));
		vo.setRes_id(this.getPara("res_id"));
		vo.setFav_order(0);
		int iCode = dao.add(vo);
		ActionResult ret;
		if (iCode > 0)
			ret = ActionResult.Valid(ValueType.SUCCESS);
		else
			ret = ActionResult.Valid(ValueType.FAILURE);
		renderJson(JSON.toJSONString(ret));
	}

	public void other() {
		ErspCodeValueRest vDict = new ErspCodeValueRest();
		List<ErspCodeValueVO> vType1 = vDict.Find(33);
		List<ErspCodeValueVO> vType3 = vDict.Find(36);
		List<ErspCodeValueVO> vType4 = vDict.Find(34);

		setAttr("typeid_1_list", vType1);
		setAttr("typeid_2_list", vType3);
		setAttr("typeid_3_list", vType4);

		render("other.ftl");
	}

	public void other_save() {
		ersp_res_info_vo voMaster = new ersp_res_info_vo();
		HttpServletRequest request = this.getRequest();
		ErspResHuman voSlave = new ErspResHuman();

		UploadPostFiles vUpload = new UploadPostFiles(this.getRequest(), this.getResponse());
		JActiveTable aMaster = RequestUtils.getFromRequest(voMaster, request);
		aMaster.setDBTableName("ersp_res_info");
		String cKeyID = Sequence.getGuid();
		aMaster.setFieldValue("id", cKeyID);
		aMaster.setFieldValue("last_update_time", String.valueOf(DateUtils.getDayTimeNum()));
		aMaster.setFieldValue("org_id", StringEx.getStr(getSessionAttr(AppConfig.SESSION_ORGID)));

		JActiveTable aSlave = RequestUtils.getFromRequest(voSlave, request);
		aSlave.setDBTableName("ersp_res_human");
		aSlave.setFieldValue("res_id", cKeyID);
		aSlave.setFieldValue("id", Sequence.getGuid());
		aSlave.setFieldValue("last_update_time", String.valueOf(DateUtils.getDayTimeNum()));

		List<UploadInfo> vUploadList = vUpload.SavePostFile(cKeyID);
		List<String> FileSQLs = vUpload.getUploadSQL(vUploadList);

		String[] sqls = new String[2 + FileSQLs.size()];
		sqls[0] = aMaster.getParmSQL();
		List<Object[]> ParmList = new ArrayList<Object[]>();
		ParmList.add(aMaster.getParmList());

		sqls[1] = aSlave.getParmSQL();
		ParmList.add(aSlave.getParmList());

		for (int i = 0; i < FileSQLs.size(); i++) {
			sqls[2 + i] = FileSQLs.get(i);
			ParmList.add(null);
		}

		int iCode = DBManager.ExecSQL(sqls, ParmList);
		ActionResult ret;

		if (iCode > 0)
			ret = ActionResult.Valid(ValueType.SUCCESS);
		else
			ret = ActionResult.Valid(ValueType.FAILURE);
		renderJson(JSON.toJSONString(ret));
	}

	public void online_source() {
		ErspCodeValueRest vDict = new ErspCodeValueRest();
		List<ErspCodeValueVO> vType1 = vDict.Find(33);
		List<ErspCodeValueVO> vType2 = vDict.Find(42);
		List<ErspCodeValueVO> vType3 = vDict.Find(39);
		List<ErspCodeValueVO> vType4 = vDict.Find(40);

		setAttr("typeid_1_list", vType1);
		setAttr("typeid_2_list", vType2);
		setAttr("typeid_3_list", vType3);
		setAttr("typeid_4_list", vType4);

		render("class_sourse.ftl");
	}

	public void dzhzy() {
		render("dzhzy.ftl");
	}

	public void Shel_for_approval() {
		render("Shel_for_approval.ftl");
	}

	public void left_2() {
		render("left_2.ftl");
	}

	public void left_3() {
		render("left_3.ftl");
	}

	public void left_4() {
		render("left_4.ftl");
	}
	public void allreq(){
		ResRequest r=new ResRequest();
		r.request(this,"myspace.ftl","all");
	}
	public void reqing() {
		ResRequest r=new ResRequest();
		r.request(this, "myspace1.ftl","0");
	}

	public void preuse() {
		ResRequest r=new ResRequest();
		r.request(this, "myspace2.ftl","1");
	}

	public void evaluat() {
		ResRequest r=new ResRequest();
		r.request(this, "myspace3.ftl","2");
	}

}
